package com.product.trace.service.impl;

import java.util.List;

import com.product.common.utils.DateUtils;
import com.product.common.utils.SecurityUtils;
import com.product.trace.VO.UniquenessIntoVO;
import com.product.trace.mapper.ProductIntoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.product.trace.mapper.ProductRetailMapper;
import com.product.trace.domain.ProductRetail;
import com.product.trace.service.IProductRetailService;

/**
 * 商品零售详情Service业务层处理
 *
 * @author wanghui
 * @date 2022-08-23
 */
@Service
public class ProductRetailServiceImpl implements IProductRetailService {
    @Autowired
    private ProductRetailMapper productRetailMapper;
    @Autowired
    private ProductIntoMapper productIntoMapper;

    /**
     * 查询商品零售详情
     *
     * @param id 商品零售详情主键
     * @return 商品零售详情
     */
    @Override
    public ProductRetail selectProductRetailById(Integer id) {
        return productRetailMapper.selectProductRetailById(id);
    }

    /**
     * 查询商品零售详情列表
     *
     * @param productRetail 商品零售详情
     * @return 商品零售详情
     */
    @Override
    public List<ProductRetail> selectProductRetailList(ProductRetail productRetail) {
        return productRetailMapper.selectProductRetailList(productRetail);
    }

    /**
     * 新增商品零售详情
     *
     * @param productRetail 商品零售详情
     * @return 结果
     */
    @Override
    public int insertProductRetail(ProductRetail productRetail) {
        productRetail.setCreateTime(DateUtils.getNowDate());
        //获取用户id
        String userid = String.valueOf(SecurityUtils.getUserId());
        //获取用户姓名
        String username = SecurityUtils.getUsername();
        //填充商户id
        productRetail.setStaffId(userid);
        //填充商户姓名
        productRetail.setCommodityStaff(username);
        return productRetailMapper.insertProductRetail(productRetail);
    }

    /**
     * 修改商品零售详情
     *
     * @param productRetail 商品零售详情
     * @return 结果
     */
    @Override
    public int updateProductRetail(ProductRetail productRetail) {
        //除过监管人员，其他人员禁用修改，如果需要修改，必须提交申请，通过监管部门进行信息修改
        //填充监管人员id
        productRetail.setCheckId(String.valueOf(SecurityUtils.getUserId()));
        //填充监管人员姓名
        productRetail.setCheckStaff(SecurityUtils.getUsername());
        //判断零售商是否通过审核
        if (productRetail.getCommodityStatus().trim().equals("0")) {
            //获取通过审核后的唯一标识和二维码
            String oid = productRetail.getCommodityId();
            UniquenessIntoVO uniquenessIntoVO = productIntoMapper.getSecturityCodeAndUUID(oid);
            productRetail.setSecturityCode(uniquenessIntoVO.getSecturityCode());
            productRetail.setCommodityUuid(uniquenessIntoVO.getCommodityUuid());
        }
        productRetail.setUpdateTime(DateUtils.getNowDate());
        return productRetailMapper.updateProductRetail(productRetail);
    }

    /**
     * 批量删除商品零售详情
     *
     * @param ids 需要删除的商品零售详情主键
     * @return 结果
     */
    @Override
    public int deleteProductRetailByIds(Integer[] ids) {
        return productRetailMapper.deleteProductRetailByIds(ids);
    }

    /**
     * 删除商品零售详情信息
     *
     * @param id 商品零售详情主键
     * @return 结果
     */
    @Override
    public int deleteProductRetailById(Integer id) {
        return productRetailMapper.deleteProductRetailById(id);
    }
}
